<?php
include dirname(dirname(__FILE__)).'/admin-verify.php';
include dirname(dirname(__FILE__)).'/user-manage/functions.php';
include dirname(__FILE__).'/functions.php';
include dirname(__FILE__).'/generate-duty-table.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';
// $duty_user_list = getDutyUser();
// $show_list = '<option vaule="0"></option>';
// foreach ($duty_user_list as $key => $value) {
// 	$show_list.='<option value="'.$key.'">'.$value.'</option>';
// }


?>

<!DOCTYPE html>
<html>
	<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php'; ?>
		<script type="text/javascript">
			$(document).ready(function() {
				$('#auto-generate').popover({
				    trigger : 'hover'
				});
				$("#auto-generate").click(function () {
					$(this).val("1");
					$("#submit").val("0");
					return true;
				});
				$("#submit").click(function () {
					$(this).val("1");
					$("#auto-generate").val("0");
					return true;
				}); 
				<?php 
					if(isset($_POST['submit'])&&$_POST['submit']=="1") {
						$table = array();
						for($i=1;$i<=7;$i++) {
							foreach(range('a','f') as $item ) {
								$table[$i][$item]=$_POST[$i.'_'.$item];
							}
						}
						if(updateTable(json_encode($table))) {
							echo 'alert("修改值班表成功！");
							window.location="'.getHost().'/admin/admin-home.php";';
						}
					} else {
						if(isset($_POST['auto-generate'])&&$_POST['auto-generate']=="1") {
							$duty_table = getGenerateTable();
						} else {
							$duty_table = getDutyTable();
						}
						for($i=1;$i<=7;$i++) {
							foreach(range('a', 'f') as $item) {
								if($duty_table[$i][$item]!="0") {
									echo '$("#'.$i.'_'.$item.' option[value=\''.$duty_table[$i][$item].'\']").attr("selected",true);';
								}
							}
						}
					}
				?>
			});
		</script>
		<link rel="stylesheet" type="text/css" href="../css/style.css">
	</head>
	<body>
		<div class="col-md-12">
			<div>
				<h3> 值班表管理</h3>
			</div>
			<div>
				<span>注：修改后的值班表将在下周生效</span>
				<form class="form-horizontal" role="form" method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>
				        <table class="col-md-10 table table-striped table-bordered table-hover table-condensed default-table">
						  <tr>
						  	<th>#</th>
						  	<th>星期一</th>
						  	<th>星期二</th>
						  	<th>星期三</th>
						  	<th>星期四</th>
						  	<th>星期五</th>
						  	<th>星期六</th>
						  	<th>星期天</th>
						  </tr>
						  <?php print_option_list(); ?>
						</table>
						<button class="btn btn-default " name="auto-generate" id="auto-generate" value="1"
						 data-placement="top" data-content="系统将根据值班人员的可值班时间段自动生成一种可行的值班方案">自动生成值班表</button>
						<button class="btn btn-primary" name="submit" id="submit" value="1" style="margin-left:20px;">确认修改值班表</button>
				</form>
			</div>
		</div>	
	</body>
</html>